'''
购物车模块用例
共分为 增删改查
该 case为 修改购物车
修改商品数量 更新 清空 购物车
'''

import unittest
from common.Base import Base, open_browser
from Page.shopping_process_page import shopping_process
from common.databaseTool import DatabaseTool
import hashlib
from time import sleep


# 登录页面url
url = "http://localhost:8080/ecshop/user.php"



class Shopingcart_case(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.data = DatabaseTool("168168", "ecshop_db", port=3308)
        sql = f"insert into ecs_users (user_name,password) values ('test','{hashlib.md5(b'111111').hexdigest()}')"
        cls.data.execute_one(sql)

    @classmethod
    def tearDownClass(cls):
        sql = "delete from ecs_users where user_name like '%test%'"
        cls.data.execute_one(sql)

    def setUp(self):
        pass
        # 每一个用例执行前执行 准备测试数据
        self.driver = open_browser()
        self.shop = shopping_process(self.driver)
        self.shop.open_url(url)
        self.shop.input_username("test")
        self.shop.input_password("111111")
        self.shop.click_submit()
        self.shop.click_homepage()

    def tearDown(self):
        # 每一个用例执行后执行 清除环境
        # 关闭浏览器
        self.shop.quit()
        pass
    ''' 用例'''
    def test_shopping_update_025(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 修改输入框的值
        self.shop.update_goods_number("2")
        # 点击更新购物车
        self.shop.click_refresh_goods()
        # 点击进入购物车
        self.shop.click_shoppingcar()
        goods_number = self.shop.goods_number()
        self.assertEqual(goods_number, "2", msg="用例执行失败 商品数量未被修改")
    def test_shopping_update_026(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 修改输入框的值
        self.shop.update_goods_number("?")
        # 点击更新购物车
        self.shop.click_refresh_goods()
        # 点击进入购物车
        self.shop.click_shoppingcar()
        goods_number = self.shop.goods_number()
        self.assertEqual(goods_number, "1", msg="用例执行失败 商品数量未被修改")

    def test_shopping_update_027(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 修改输入框的值
        self.shop.update_goods_number("二")
        # 点击更新购物车
        self.shop.click_refresh_goods()
        # 点击进入购物车
        self.shop.click_shoppingcar()
        goods_number = self.shop.goods_number()
        self.assertEqual(goods_number, "1", msg="用例执行失败 商品数量未被修改")

    def test_shopping_update_028(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 修改输入框的值
        self.shop.update_goods_number("ten")
        # 点击更新购物车
        self.shop.click_refresh_goods()
        # 点击进入购物车
        self.shop.click_shoppingcar()
        #断言
        goods_number = self.shop.goods_number()
        self.assertEqual(goods_number, "1", msg="用例执行失败 商品数量未被修改")
    def test_shopping_update_029(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 修改输入框的值
        self.shop.update_goods_number("三")
        # 点击更新购物车
        self.shop.click_refresh_goods()
        # 点击进入购物车
        self.shop.click_shoppingcar()
        goods_number = self.shop.goods_number()
        self.assertEqual(goods_number, "1", msg="用例执行失败 商品数量未被修改")
    def test_shopping_update_030(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 修改输入框的值
        self.shop.update_goods_number("2.2")
        # 点击更新购物车
        self.shop.click_refresh_goods()
        # 点击进入购物车
        self.shop.click_shoppingcar()
        goods_number = self.shop.goods_number()
        self.assertEqual(goods_number, "2", msg="用例执行失败 商品数量未被修改,或修改不正确")

    def test_shopping_update_031(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 点击继续购物
        self.shop.click_continue_shopping()
        # 判断是否回到首页
        index_title = "ECSHOP演示站 - Powered by ECShop"
        self.assertEqual(self.shop.title(), index_title, "用例执行失败,未跳转到首页")

    def test_shopping_update_032(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 点击继续购物
        self.shop.click_continue_shopping()
        # 点击点击炫彩翻页保护套商品()
        self.shop.click_Colorfulflipcover()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 判断购物车数量是否为2
        judge=self.shop.shoppingcart_judge_number("购物车(2)")
        self.assertTrue(judge,msg="用例执行失败 购物车数量不符合")

    def test_shopping_update_033(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 点击继续购物
        self.shop.click_continue_shopping()
        # 点击点击炫彩翻页保护套商品()
        self.shop.click_Colorfulflipcover()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 点击清空购物车按钮
        self.shop.click_clear_goods()
        try:
            shoppingcart_title = "购物流程_ECSHOP演示站 - Powered by ECShop"
            self.assertEqual(self.shop.title(), shoppingcart_title, msg="用例执行失败不在购物车页面")
        except:

            self.assertTrue(False, msg="用例执行失败不在购物车页面")

    def test_shopping_update_034(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 点击继续购物
        self.shop.click_continue_shopping()
        # 点击炫彩翻页保护套商品
        self.shop.click_Colorfulflipcover()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 点击清空购物车按钮
        self.shop.click_clear_goods()

        try:
            # 获取弹框点击
            self.shop.click_window()
            #获取到弹框断言
            self.assertTrue(True)
        except:
            #未获取到弹框断言
            self.assertTrue(False,"未找到弹框,或没有弹框")

    def test_shopping_update_037(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 点击继续购物
        self.shop.click_continue_shopping()
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 判断购物车数量是否为2
        judge=self.shop.shoppingcart_judge_number("购物车(2)")
        self.assertTrue(judge,msg="用例执行失败 购物车数量不符合")